/**
 * Copyright (c) 2009, http://www.softwareinlife.com. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided that 
 * the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the http://www.softwareinlife.com nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE SOFTWAREINLIFE AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.softwareinlife.vision.android.communication.model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlSerializer;

import com.softwareinlife.vision.android.system.model.BaseModel;

/**
 * @author Cho, jae-young (greenfrog@softwareinlife.com)
 * @date 2009. 10. 19.
 */
public class VisionSoft implements IXmlWriter {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 
	 */
	public static final String ELEMENT = "visionsoft";

	/**
	 * 
	 */
	private List<UserModel> m_userModels = new ArrayList<UserModel>();
	
	/**
	 * 
	 */
	private List<VisionModel> m_visionModels = new ArrayList<VisionModel>();
	
	/**
	 * 
	 */
	private List<PlanModel> m_planModels = new ArrayList<PlanModel>();
	
	/**
	 * 
	 */
	private List<ScheduleModel> m_scheduleModels = new ArrayList<ScheduleModel>();
	
	/**
	 * 
	 */
	private List<EventModel> m_eventModels = new ArrayList<EventModel>();
	
	/**
	 * 
	 * @param arg_model
	 */
	public void AddUserModel(UserModel arg_model)
	{
		m_userModels.add(arg_model);
	}
	
	/**
	 * 
	 * @param arg_model
	 */
	public void AddVisionModel(VisionModel arg_model)
	{
		m_visionModels.add(arg_model);
	}
	
	/**
	 * 
	 * @param arg_model
	 */
	public void AddPlanModel(PlanModel arg_model)
	{
		m_planModels.add(arg_model);
	}
	
	/**
	 * 
	 * @param arg_model
	 */
	public void AddScheduleModel(ScheduleModel arg_model)
	{
		m_scheduleModels.add(arg_model);
	}
	
	/**
	 * 
	 * @param arg_model
	 */
	public void AddEventModel(EventModel arg_model)
	{
		m_eventModels.add(arg_model);
	}
	
	/**
	 * 
	 * @return
	 */
	public List<VisionModel> getVisionModel()
	{
		return m_visionModels;
	}
	
	/**
	 * 
	 * @param <T>
	 * @param arg_serializer
	 * @param arg_models
	 * @param arg_tags
	 * @throws IllegalArgumentException
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	public <T> void childElementToXmlString(XmlSerializer arg_serializer, List<T> arg_models,
			String arg_tags)
			throws IllegalArgumentException, IllegalStateException, IOException
	{
		if(0 < arg_models.size())
			arg_serializer.startTag("", arg_tags);
		
		for(T childItem : arg_models) 
		{
			IXmlWriter xmlWriter = (IXmlWriter)childItem;
			xmlWriter.toXMLString(arg_serializer);
		}
		
		if(0 < arg_models.size())
			arg_serializer.endTag("", arg_tags);
	}
	
	/**
	 * 
	 * 
	 * @param arg_serializer
	 */
	public void toXMLString(XmlSerializer arg_serializer) 
			throws IllegalArgumentException, IllegalStateException, IOException
	{
		// TODO Auto-generated method stub
		arg_serializer.startTag("", ELEMENT);

		childElementToXmlString(arg_serializer, m_userModels, "users");
		childElementToXmlString(arg_serializer, m_visionModels, "visions");
		childElementToXmlString(arg_serializer, m_planModels, "plans");
		childElementToXmlString(arg_serializer, m_scheduleModels, "schedules");
		childElementToXmlString(arg_serializer, m_eventModels, "events");

				
		arg_serializer.endTag("", ELEMENT);
	}
}